function dispslicemovie(voldata, sdir, pauseval)
% dispslicemovie  - display slice movie
%
% FORMAT:       dispslicemovie(voldata, [dir [, pauseval]])
%
% Input fields:
%
%       voldata     3-D dataset
%       dir         either of [-3, -2, -1, 1, 2, 3]
%       pauseval    value to pause between two slices (default: 0.04)
%
% See also dispslice

% Version:  v0.7g
% Build:    9031922
% Date:     Mar-19 2009, 10:00 PM CET
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 1 || ...
    isempty(voldata)
    error( ...
        'BVQXtools:BadArgument', ...
        'Bad or missing input argument provided.' ...
    );
end

% resolve transio if necessary
if istransio(voldata)
    voldata = resolve(voldata);
end

% get size and necessary values
vsz = size(voldata);
if numel(vsz) < 3
    vsz(3) = 1;
end
mmm = minmaxmean(voldata, 4);
vmn = mmm(1);
vmx = mmm(2);

% no/bad direction/pauseval given
if nargin < 2 || ...
   ~isa(sdir, 'double') || ...
    numel(sdir) ~= 1 || ...
   ~any(sdir == [-4:-1, 1:4]) || ...
    abs(sdir) > numel(vsz)
    sdir = 3;
end
if nargin < 3 || ...
   ~isa(pauseval, 'double') || ...
    numel(pauseval) ~= 1 || ...
    isinf(pauseval) || ...
    isnan(pauseval) || ...
    pauseval < 0 || ...
    pauseval > 2
    pauseval = 0.04;
end

% for 4-D data, reshape to a good set
if abs(sdir) == 4
    voldata = reshape(permute(voldata, [1, 2, 4, 3]), [vsz(1:2), vsz(3) * vsz(4)]);
    sdir = 3 * sign(sdir);
elseif numel(vsz) > 3
    voldata = reshape(voldata, [vsz(1:2), vsz(3) * vsz(4)]);
end
vsz = size(voldata);

% delete dispslice figure
dispslice closefig;

% slice preselection is empty
slc = [];

% more than single direction given
if numel(sdir) > 1
    slc = sdir(2:end);
    sdir = sdir(1);
end

% reject invalid dir's
if ~any([-3:-1, 1:3] == sdir)
    error( ...
        'BVQXtools:BadArgument', ...
        'Invalid dir argument.' ...
    );
end

% if negative go backwards
reversed = false;
if sdir < 0
    sdir = -sdir;
    reversed = true;
end

% force good slices
slc = intersect(slc, 1:vsz(sdir));

% preselection empty ?
if isempty(slc)

    % all slices
    slc = 1:vsz(sdir);
end

% reversed
if reversed
    slc = slc(end:-1:1);
end

% get slice size
switch (sdir)
    case {1}
        ssz = vsz(2:3);
    case {2}
        ssz = vsz([1, 3]);
    otherwise
        ssz = vsz(1:2);
end
ssx = vsz(sdir);

% create empty dispslice
dispslice(zeros(ssz));

% get image handle
ih = dispslice('image');

% display requested slices
for sl = slc
    switch (sdir)
        case {1}
            dispslice(voldata(sl, :, :), ih, [vmn, vmx]);
        case {2}
            dispslice(voldata(:, sl, :), ih, [vmn, vmx]);
        otherwise
            dispslice(voldata(:, :, sl), ih, [vmn, vmx]);
    end
    dispslice(sprintf('Dispslicemovie: slice %d/%d', sl, ssx));
    pause(pauseval);
end
